package com.yq.servlet;

import com.sun.xml.internal.ws.api.server.LazyMOMProvider;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/studentUpload")
public class StudentUploadServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //判断是否有文件的提交
        boolean multipartContent = ServletFileUpload.isMultipartContent(request);
        if (!multipartContent){
            return;
        }
        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletContext servletContext = getServletContext();
        File respository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
        factory.setRepository(respository);
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setHeaderEncoding("utf-8");
        upload.setSizeMax(1000000);
        try {
            List<FileItem> fileItems = upload.parseRequest(request);
            for (FileItem fileItem : fileItems) {
                if (fileItem.isFormField()){
                    System.out.println("正常的表单文件不做处理");
                }else {
                    processFileSave(fileItem);
                    response.getWriter().println("file id upload success....");
                    response.setHeader("refresh","2;url="+request.getContextPath()+"");
                }
            }
        } catch (FileUploadException e) {
            e.printStackTrace();
        }

    }

    private void processFileSave(FileItem fileItem) {
        ServletContext servletContext = getServletContext();
        String name = fileItem.getName();

        //设置提交作业的时间日期
        String date = new SimpleDateFormat("MM-dd").format(new Date());


        String realPath = servletContext.getRealPath("homework/"+date+"/"+name);
        File file = new File(realPath);
        if (file.getParentFile().exists()){
            file.getParentFile().mkdirs();
        }
//
//        //设置一个全局的共享目录，也就是作业提交的目录
//        servletContext.setAttribute("homeworkdir",servletContext.getRealPath("homework/"+date));



        //重复提交删除前边提交的版本
        if (file.exists()){
            file.delete();
        }
        try {
            fileItem.write(file);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
